﻿#include "FITKNastranBDFIOInterface.h"
#include "FITKNastranBDFReader.h"
#include "FITKNastranBDFWriter.h"

#include <QThread>

namespace Nastran
{
    bool FITKNastranBDFIOInterface::exec(const int indexPort)
    {
        //如果正在执行，则退出，不能同时执行多个任务
        _success = false;
        if (this->isRunning()) return false;

        switch (indexPort)
        {
        case 1:  //读取
            _io = new FITKNastranBDFReader(_success);
            break;
        case 2:  //写出
            _io = new FITKNastranBDFWriter(_success);
            break;
        default: break;
        }
        //参数错误，直接退出
        if (_io == nullptr) return false;
        _io->setDataObject(this->getDataObject("Model"));
        _io->setFileName(_fileName);
        //在线程中执行
        if (_runInThread)
        {
            connect(_io.getObjectPtr(), SIGNAL(taskFinishedSig(FITKThreadTask*)), this, SIGNAL(ioThreadFinishedSig()));
            _io->push2ThreadPool();
            QThread::msleep(100);
        }
        //不在线程执行
        else
        {
            _io->run();
            auto p = _io.getObjectPtr();
            if (p) delete p;
        }
        
        //参数传递完成，清空参数
        this->resetDataObjects();
        return _success;
    }

    void FITKNastranBDFIOInterface::setScaleFactor(double v)
    {
        _scaleFactor = v;
    }

    void FITKNastranBDFIOInterface::setFileName(const QString& fileName)
    {
        _fileName = fileName;

    }

    void FITKNastranBDFIOInterface::runInThread(const bool & inThread /*= true*/)
    {
        _runInThread = inThread;
    }

    bool FITKNastranBDFIOInterface::isRunning()
    {
        return _io != nullptr;
    }

    bool FITKNastranBDFIOInterface::isSuccess()
    {
        return _success;
    }

    QString FITKNastranBDFIOInterface::getComponentName()
    {
        return "NastranBDFIO";
    }
}


